<!DOCTYPE html>
<html>
<!-- Created by GNU Texinfo 7.1, https://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!-- Copyright © 1988-2023 Free Software Foundation, Inc.

Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being "Funding Free Software", the Front-Cover
Texts being (a) (see below), and with the Back-Cover Texts being (b)
(see below).  A copy of the license is included in the section entitled
"GNU Free Documentation License".

(a) The FSF's Front-Cover Text is:

A GNU Manual

(b) The FSF's Back-Cover Text is:

You have freedom to copy and modify this GNU Manual, like GNU
     software.  Copies published by the Free Software Foundation raise
     funds for GNU development. -->
<title>Output Statement (GNU Compiler Collection (GCC) Internals)</title>

<meta name="description" content="Output Statement (GNU Compiler Collection (GCC) Internals)">
<meta name="keywords" content="Output Statement (GNU Compiler Collection (GCC) Internals)">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta name="viewport" content="width=device-width,initial-scale=1">

<link href="index.html" rel="start" title="Top">
<link href="Option-Index.html" rel="index" title="Option Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Machine-Desc.html" rel="up" title="Machine Desc">
<link href="Predicates.html" rel="next" title="Predicates">
<link href="Output-Template.html" rel="prev" title="Output Template">
<style type="text/css">
<!--
a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em}
div.example {margin-left: 3.2em}
span:hover a.copiable-link {visibility: visible}
-->
</style>


</head>

<body lang="en">
<div class="section-level-extent" id="Output-Statement">
<div class="nav-panel">
<p>
Next: <a href="Predicates.html" accesskey="n" rel="next">Predicates</a>, Previous: <a href="Output-Template.html" accesskey="p" rel="prev">Output Templates and Operand Substitution</a>, Up: <a href="Machine-Desc.html" accesskey="u" rel="up">Machine Descriptions</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<h3 class="section" id="C-Statements-for-Assembler-Output"><span>17.6 C Statements for Assembler Output<a class="copiable-link" href="#C-Statements-for-Assembler-Output"> &para;</a></span></h3>
<a class="index-entry-id" id="index-output-statements"></a>
<a class="index-entry-id" id="index-C-statements-for-assembler-output"></a>
<a class="index-entry-id" id="index-generating-assembler-output"></a>

<p>Often a single fixed template string cannot produce correct and efficient
assembler code for all the cases that are recognized by a single
instruction pattern.  For example, the opcodes may depend on the kinds of
operands; or some unfortunate combinations of operands may require extra
machine instructions.
</p>
<p>If the output control string starts with a &lsquo;<samp class="samp">@</samp>&rsquo;, then it is actually
a series of templates, each on a separate line.  (Blank lines and
leading spaces and tabs are ignored.)  The templates correspond to the
pattern&rsquo;s constraint alternatives (see <a class="pxref" href="Multi_002dAlternative.html">Multiple Alternative Constraints</a>).  For example,
if a target machine has a two-address add instruction &lsquo;<samp class="samp">addr</samp>&rsquo; to add
into a register and another &lsquo;<samp class="samp">addm</samp>&rsquo; to add a register to memory, you
might write this pattern:
</p>
<div class="example smallexample">
<pre class="example-preformatted">(define_insn &quot;addsi3&quot;
  [(set (match_operand:SI 0 &quot;general_operand&quot; &quot;=r,m&quot;)
        (plus:SI (match_operand:SI 1 &quot;general_operand&quot; &quot;0,0&quot;)
                 (match_operand:SI 2 &quot;general_operand&quot; &quot;g,r&quot;)))]
  &quot;&quot;
  &quot;@
   addr %2,%0
   addm %2,%0&quot;)
</pre></div>

<a class="index-entry-id" id="index-_002a-in-template"></a>
<a class="index-entry-id" id="index-asterisk-in-template"></a>
<p>If the output control string starts with a &lsquo;<samp class="samp">*</samp>&rsquo;, then it is not an
output template but rather a piece of C program that should compute a
template.  It should execute a <code class="code">return</code> statement to return the
template-string you want.  Most such templates use C string literals, which
require doublequote characters to delimit them.  To include these
doublequote characters in the string, prefix each one with &lsquo;<samp class="samp">\</samp>&rsquo;.
</p>
<p>If the output control string is written as a brace block instead of a
double-quoted string, it is automatically assumed to be C code.  In that
case, it is not necessary to put in a leading asterisk, or to escape the
doublequotes surrounding C string literals.
</p>
<p>The operands may be found in the array <code class="code">operands</code>, whose C data type
is <code class="code">rtx []</code>.
</p>
<p>It is very common to select different ways of generating assembler code
based on whether an immediate operand is within a certain range.  Be
careful when doing this, because the result of <code class="code">INTVAL</code> is an
integer on the host machine.  If the host machine has more bits in an
<code class="code">int</code> than the target machine has in the mode in which the constant
will be used, then some of the bits you get from <code class="code">INTVAL</code> will be
superfluous.  For proper results, you must carefully disregard the
values of those bits.
</p>
<a class="index-entry-id" id="index-output_005fasm_005finsn"></a>
<p>It is possible to output an assembler instruction and then go on to output
or compute more of them, using the subroutine <code class="code">output_asm_insn</code>.  This
receives two arguments: a template-string and a vector of operands.  The
vector may be <code class="code">operands</code>, or it may be another array of <code class="code">rtx</code>
that you declare locally and initialize yourself.
</p>
<a class="index-entry-id" id="index-which_005falternative"></a>
<p>When an insn pattern has multiple alternatives in its constraints, often
the appearance of the assembler code is determined mostly by which alternative
was matched.  When this is so, the C code can test the variable
<code class="code">which_alternative</code>, which is the ordinal number of the alternative
that was actually satisfied (0 for the first, 1 for the second alternative,
etc.).
</p>
<p>For example, suppose there are two opcodes for storing zero, &lsquo;<samp class="samp">clrreg</samp>&rsquo;
for registers and &lsquo;<samp class="samp">clrmem</samp>&rsquo; for memory locations.  Here is how
a pattern could use <code class="code">which_alternative</code> to choose between them:
</p>
<div class="example smallexample">
<pre class="example-preformatted">(define_insn &quot;&quot;
  [(set (match_operand:SI 0 &quot;general_operand&quot; &quot;=r,m&quot;)
        (const_int 0))]
  &quot;&quot;
  {
  return (which_alternative == 0
          ? &quot;clrreg %0&quot; : &quot;clrmem %0&quot;);
  })
</pre></div>

<p>The example above, where the assembler code to generate was
<em class="emph">solely</em> determined by the alternative, could also have been specified
as follows, having the output control string start with a &lsquo;<samp class="samp">@</samp>&rsquo;:
</p>
<div class="example smallexample">
<div class="group"><pre class="example-preformatted">(define_insn &quot;&quot;
  [(set (match_operand:SI 0 &quot;general_operand&quot; &quot;=r,m&quot;)
        (const_int 0))]
  &quot;&quot;
  &quot;@
   clrreg %0
   clrmem %0&quot;)
</pre></div></div>

<p>If you just need a little bit of C code in one (or a few) alternatives,
you can use &lsquo;<samp class="samp">*</samp>&rsquo; inside of a &lsquo;<samp class="samp">@</samp>&rsquo; multi-alternative template:
</p>
<div class="example smallexample">
<div class="group"><pre class="example-preformatted">(define_insn &quot;&quot;
  [(set (match_operand:SI 0 &quot;general_operand&quot; &quot;=r,&lt;,m&quot;)
        (const_int 0))]
  &quot;&quot;
  &quot;@
   clrreg %0
   * return stack_mem_p (operands[0]) ? \&quot;push 0\&quot; : \&quot;clrmem %0\&quot;;
   clrmem %0&quot;)
</pre></div></div>

</div>
<hr>
<div class="nav-panel">
<p>
Next: <a href="Predicates.html">Predicates</a>, Previous: <a href="Output-Template.html">Output Templates and Operand Substitution</a>, Up: <a href="Machine-Desc.html">Machine Descriptions</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html" title="Index" rel="index">Index</a>]</p>
</div>



</body>
</html>
